import { remove, getList, save } from '@/api/park/info'
import { getSelects } from '@/api/house/community'
import { getBuildingSelects } from '@/api/house/building'
import { getRoomSelects } from '@/api/house/room'
import { getOptions } from '@/api/option'
import { getAllSelectors, saveAll } from '@/api/park/ref'

export default {
  data() {
    return {
      formVisible: false,
      formTitle: '添加车位信息',
      isAdd: true,
      form: {
        communityId:'',
        buildingId:'',
        roomId:'',
        code:'',
        name:'',
        position:'',
        ownerFlag:'',
        parkingType:'',
        id: '',
        createBy:'',
        createTime:''
      },
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      options:{
        parkOwnerFlag:[],
        parkType:[],
        communitys:[],
        buildings:[],
        rooms:[]
      },
      viewCust:false,
      quote:{
        quoteTitle: '报价列表',
        quoteVisible: false,
        list:[],
        listLoading: true
      },
      viewRuleDisabled:true,
      viewRuleDisabled1:false,
      parkingId:'',
      checkedQuotes:[],
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  computed: {

    //表单验证
    rules() {
      return {
        communityId: [
          { required: true, message: '请选择社区', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入编号', trigger: 'blur' },
          { min: 2, max: 64, message: '长度在 2 到 16 个字符', trigger: 'blur' }
        ],
        name:[
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 64, message: '长度在 2 到 128 个字符', trigger: 'blur' }
        ],
        position:[
          { required: true, message: '请输入位置', trigger: 'blur' },
          { min: 2, max: 64, message: '长度在 2 到 128 个字符', trigger: 'blur' }
        ],
        parkingType:[
          { required: true, message: '请选择车位类型', trigger: 'blur' }
        ],
        ownerFlag:[
          { required: true, message: '请选择租售方式', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.init()
    this.initOptions()
    this.initCommunitys()
  },
  methods: {
    init() {
      this.fetchData()
    },
    initOptions(){
      getOptions('ParkOwnerFlag_ParkType').then(response => {
        this.options.parkOwnerFlag = response.data.maps.ParkOwnerFlag;
        this.options.parkType = response.data.maps.ParkType;
      })
    },
    initCommunitys(){
      getSelects().then(response => {
        this.options.communitys = response.data;
      })
    },
    getBuildings(){
      this.form.buildingId = '';
      this.form.roomId = '';
      this.options.rooms = [];
      getBuildingSelects(this.form.communityId).then(response => {
        this.options.buildings = response.data;
      })
    },
    getRooms(){
      this.form.roomId = '';
      getRoomSelects(this.form.buildingId).then(response => {
        this.options.rooms = response.data;
      })
    },
    getCusts(){
      if(this.form.ownerFlag > 0){
        this.viewCust = true;
      }else{
        this.viewCust = false;
      }
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    search() {
      this.fetchData()
    },
    reset() {
      this.listQuery.id = ''
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    resetForm() {
      this.form = {
        communityId:'',
        buildingId:'',
        roomId:'',
        code:'',
        name:'',
        position:'',
        ownerFlag:'',
        parkingType:'',
        id: ''
      }
    },
    add() {
      this.resetForm()
      this.formTitle = '添加车位信息',
      this.formVisible = true
      this.isAdd = true
    },
    quoteList(communityId, parkingId){
      this.quote.quoteTitle = "报价列表"
      this.quote.quoteVisible = true;
      this.viewRuleDisabled = true;
      this.viewRuleDisabled1 = false;
      getAllSelectors(communityId, parkingId).then(response => {
        this.quote.list = response.data
        this.quote.listLoading = false
      })
    },
    configRule(communityId, parkingId){
      this.quote.quoteTitle = "报价配置"
      this.checkedQuotes = [];
      this.quote.quoteVisible = true;
      this.viewRuleDisabled = false;
      this.viewRuleDisabled1 = true;
      getAllSelectors(communityId, parkingId).then(response => {
        this.parkingId = parkingId
        this.quote.list = response.data
        this.quote.listLoading = false
        this.quote.list.forEach((item, index) => {
          if(item.check01){
            this.checkedQuotes.push(item.id);
          }
        })
      })
    },
    changeConfig(r){
      let tp = this.checkedQuotes;
      if(!r.check01){
        if(r.check02){
          this.$notify.error({
            title: '错误',
            message: '不能去除已选中的方案'
          })
        }else{
          tp.forEach(function(item, index, arr) {
            if(r.id === item) {
              tp.splice(index, 1);
            }
          });
        }
        this.checkedQuotes = tp;
      }else{
          this.checkedQuotes.push(r.id);
      }
    },
    saveConfig(){
      let ids = ''
      this.checkedQuotes.forEach(function(item, index) {
        ids += item + "_";
      });
      saveAll({
        parkingId: this.parkingId,
        ids: ids
      }).then(response => {
        this.$message({
          message: this.$t('common.optionSuccess'),
          type: 'success'
        })
        this.quote.quoteVisible = false
      })
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          save({
            communityId:this.form.communityId,
            buildingId:this.form.buildingId === '' ? 0 : this.form.buildingId,
            roomId:this.form.roomId === '' ? 0 : this.form.roomId,
            code:this.form.code,
            name:this.form.name,
            position:this.form.position,
            ownerFlag:this.form.ownerFlag,
            parkingType:this.form.parkingType,
            id: this.form.id,
            createBy:this.form.createBy,
            createTime: this.form.createTime
          }).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
            this.formVisible = false
          })
        } else {
          return false
        }
      })
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    edit() {
      if (this.checkSel()) {
        this.isAdd = false
        this.form = this.selRow
        this.formTitle = '编辑车位信息'
        this.formVisible = true
      }
    },
    remove() {
      if (this.checkSel()) {
        var id = this.selRow.id
        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning'
        }).then(() => {
          remove(id).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
          }).catch( err=> {
            this.$notify.error({
              title: '错误',
              message: err
            })
          })
        }).catch(() => {
        })
      }
    }

  }
}
